// pages/manageData/manageData.js

// 使用云数据库
const db = wx.cloud.database();

// 引入获取当前时间类
const util = require('util.js');


Page({

    /**
     * 页面的初始数据
     */
    data: {
        temp_hotList:[],
        hotList:[],
        temp_lawsList:[],
        lawsList:[],
        temp_farmingList:[],
        farmingList:[],
        // 热门模块
        hotmodule:[
            {id:1,type:'广场舞',value:'广场舞教学',hot:0,photo:''},
            {id:2,type:'书法',value:'书法展示',hot:0,photo:''},
            {id:3,type:'国画',value:'国画鉴赏',hot:0,photo:''},
            {id:4,type:'彩陶',value:'陶艺历史',hot:0,photo:''},
            {id:5,type:'古筝',value:'古筝演奏',hot:0,photo:''},
            {id:6,type:'武术',value:'武术表演',hot:0,photo:''},
            {id:7,type:'剪纸',value:'剪纸生活',hot:0,photo:''},
        ],
        // 法律模块
        lawsmodule:[
            {id:1,type:'劳动纠纷',value:'劳动纠纷',collectionsNum:0,photo:'',info:''},
            {id:2,type:'医疗纠纷',value:'医疗纠纷',collectionsNum:0,photo:'',info:''},
            {id:3,type:'房产纠纷',value:'房产纠纷',collectionsNum:0,photo:'',info:''},
            {id:4,type:'合同纠纷',value:'合同纠纷',collectionsNum:0,photo:'',info:''},
            {id:5,type:'债权债务',value:'债权债务',collectionsNum:0,photo:'',info:''},
            {id:6,type:'交通事故',value:'交通事故',collectionsNum:0,photo:'',info:''},
            {id:7,type:'形式辩护',value:'形式辩护',collectionsNum:0,photo:'',info:''},
            {id:8,type:'婚姻家庭',value:'婚姻家庭',collectionsNum:0,photo:'',info:''},
        ],
        // 农技模块
        farmingmodule:[
            {id:1,type:'栽培柑橘',value:'栽培柑橘',playNum:0,photo:'',info:''},
            {id:2,type:'栽培葡萄',value:'栽培葡萄',playNum:0,photo:'',info:''},
            {id:3,type:'栽培西红柿',value:'栽培西红柿',playNum:0,photo:'',info:''},
            {id:4,type:'栽培苹果',value:'栽培苹果',playNum:0,photo:'',info:''},
            {id:5,type:'栽培火龙果',value:'栽培火龙果',playNum:0,photo:'',info:''},
            {id:6,type:'栽培西瓜',value:'栽培西瓜',playNum:0,photo:'',info:''},
            {id:7,type:'栽培荔枝',value:'栽培荔枝',playNum:0,photo:'',info:''},
            {id:8,type:'栽培蜜柚',value:'栽培蜜柚',playNum:0,photo:'',info:''},
            {id:9,type:'栽培辣椒',value:'栽培辣椒',playNum:0,photo:'',info:''},
            {id:10,type:'栽培黄瓜',value:'栽培黄瓜',playNum:0,photo:'',info:''},
            {id:11,type:'栽培大姜',value:'栽培大姜',playNum:0,photo:'',info:''},
            {id:12,type:'栽培马铃薯',value:'栽培马铃薯',playNum:0,photo:'',info:''},
            {id:13,type:'栽培玉米',value:'栽培玉米',playNum:0,photo:'',info:''},
            {id:14,type:'栽培白菜',value:'栽培白菜',playNum:0,photo:'',info:''},
            {id:15,type:'栽培花生',value:'栽培花生',playNum:0,photo:'',info:''},
            {id:16,type:'栽培茄子',value:'栽培茄子',playNum:0,photo:'',info:''},
        ]

    },

    // 加载热门列表数据
    updataCommunityRank:function(){
        wx.showLoading({
            title: '数据更新中...',
            mask:true
        })
        let that = this
        let promise = new Promise((reslove,reject)=>{
            db.collection('amusementVideo').count().then(async res =>{
                let total = res.total;
                // 计算需分几次取
                const batchTimes = Math.ceil(total / 10)
                 // 清空临时数组
                that.setData({
                    temp_hotList:[]
                })
                // 承载所有读操作的 promise 的数组
                for (let i = 0; i < batchTimes; i++) {
                    await db.collection('amusementVideo').skip(i * 10).limit(10).get().then(async res => {
                        let new_data = res.data
                        let old_data = that.data.temp_hotList
                        that.setData({
                            temp_hotList:old_data.concat(new_data)
                        })
                    })
                }
                reslove();
            })
        })
        promise.then(res=>{
            let temp_hotList = that.data.temp_hotList
            let hotmodule = that.data.hotmodule
            // 初始化数组长度(根据hotmodule长度而定)
            let outArr=[]
            for(var i=0;i<hotmodule.length;i++){
                outArr.push([]);
            }
            // 将数据库的temp_hotList数据进行分类存储
            for (let i = 0; i < temp_hotList.length; i++) {
                let temp_flag = temp_hotList[i].type
                for (let j = 0; j < hotmodule.length; j++) {
                    if(temp_flag === hotmodule [j].type){
                        outArr[j].push(temp_hotList[i])
                        break
                    }   
                }
            }
            // 保存分完类的列表到hotList
            that.setData({
                hotList:outArr
            })

            // 计算热度
            for(let i = 0; i<that.data.hotList.length; i++){
                // 基础热度值
                let hotCount = 1
                let tempHot = 0
                let tempHot_avatarUrl
                for(let j = 0; j<that.data.hotList[i].length; j++){
                    let single_hot = that.data.hotList[i][j].collectionsNum*10 + that.data.hotList[i][j].commentsNum*8 + that.data.hotList[i][j].goodNum*5+that.data.hotList[i][j].playNum*2
                    if(tempHot <= single_hot){
                        tempHot = single_hot
                        tempHot_avatarUrl = that.data.hotList[i][j].avatarUrl
                    }
                    hotCount += single_hot
                    // console.log(single_hot)
                }
                let temp
                let tempAvatarUrl 
                if(i === 0){
                    temp = 'hotmodule[0].hot'
                    tempAvatarUrl = 'hotmodule[0].photo'
                }
                else if(i === 1){
                    temp = 'hotmodule[1].hot'
                    tempAvatarUrl = 'hotmodule[1].photo'
                }
                else if(i === 2){
                    temp = 'hotmodule[2].hot'
                    tempAvatarUrl = 'hotmodule[2].photo'
                }
                else if(i === 3){
                    temp = 'hotmodule[3].hot'
                    tempAvatarUrl = 'hotmodule[3].photo'
                }
                else if(i === 4){
                    temp = 'hotmodule[4].hot'
                    tempAvatarUrl = 'hotmodule[4].photo'
                }
                else if(i === 5){
                    temp = 'hotmodule[5].hot'
                    tempAvatarUrl = 'hotmodule[5].photo'
                }
                else if(i === 6){
                    temp = 'hotmodule[6].hot'
                    tempAvatarUrl = 'hotmodule[6].photo'
                }
                
                that.setData({
                    [temp]: hotCount,
                    [tempAvatarUrl]:tempHot_avatarUrl
                }) 
            }
            // 进行排序
            for(let i = 0;i<that.data.hotmodule.length-1;i++){
                let tempSwap
                for(let j = i; j<that.data.hotmodule.length;j++){
                    if(that.data.hotmodule[i].hot<that.data.hotmodule[j].hot){
                        tempSwap = that.data.hotmodule[i]
                        that.data.hotmodule[i] = that.data.hotmodule[j]
                        that.data.hotmodule[j] = tempSwap
                    }
                }
            } 
        })
    },
    // 加载普法栏目数据
    updataLawsRank:function(){
        let that = this
        let promise = new Promise((reslove,reject)=>{
            db.collection('lawsVideo').count().then(async res =>{
                let total = res.total;
                // 计算需分几次取
                const batchTimes = Math.ceil(total / 10)
                 // 清空临时数组
                that.setData({
                    temp_lawsList:[]
                })
                // 承载所有读操作的 promise 的数组
                for (let i = 0; i < batchTimes; i++) {
                    await db.collection('lawsVideo').skip(i * 10).limit(10).get().then(async res => {
                        let new_data = res.data
                        let old_data = that.data.temp_lawsList
                        that.setData({
                            temp_lawsList:old_data.concat(new_data)
                        })
                    })
                }
                reslove();
            })
        })
        promise.then(res=>{
            let temp_lawsList = that.data.temp_lawsList
            let lawsmodule = that.data.lawsmodule
            // 初始化数组长度(根据lawsmodule长度而定)
            let outArr=[]
            for(let i=0;i<4;i++){
                outArr.push([]);
            }
             // 将数据库的temp_lawsList数据进行分类存储
             for (let i = 0; i < temp_lawsList.length; i++) {
                let temp_flag = temp_lawsList[i].type
                for (let j = 0; j < 4; j++) {
                    if(temp_flag === lawsmodule [j].type){
                        outArr[j].push(temp_lawsList[i])
                        break
                    }   
                }
            }
            // 保存分完类的列表到lawsList
            that.setData({
                lawsList:outArr
            })

            // 计算总的收藏量
            for(let i = 0; i<that.data.lawsList.length; i++){
                // 基础收藏量
                let collectionsCount = 0
                let tempCollections = 0
                let tempCollections_cover
                let tempCollections_info
                for(let j = 0; j<that.data.lawsList[i].length; j++){
                    let single_collectionsNum = that.data.lawsList[i][j].collectionsNum
                    if(tempCollections <= single_collectionsNum){
                        tempCollections = single_collectionsNum
                        tempCollections_cover = that.data.lawsList[i][j].upload_cover
                        tempCollections_info = that.data.lawsList[i][j].info
                    }
                    console.log("加载前"+collectionsCount)
                    collectionsCount += single_collectionsNum
                }
                console.log(collectionsCount)
                let temp
                let tempCover
                let tempInfo
                if(i === 0){
                    temp = 'lawsmodule[0].collectionsNum'
                    tempCover = 'lawsmodule[0].photo'
                    tempInfo = 'lawsmodule[0].info'
                }
                else if(i === 1){
                    temp = 'lawsmodule[1].collectionsNum'
                    tempCover = 'lawsmodule[1].photo'
                    tempInfo = 'lawsmodule[1].info'
                }
                else if(i === 2){
                    temp = 'lawsmodule[2].collectionsNum'
                    tempCover = 'lawsmodule[2].photo'
                    tempInfo = 'lawsmodule[2].info'
                }
                else if(i === 3){
                    temp = 'lawsmodule[3].collectionsNum'
                    tempCover = 'lawsmodule[3].photo'
                    tempInfo = 'lawsmodule[3].info'
                }
                else if(i === 4){
                    temp = 'lawsmodule[4].collectionsNum'
                    tempCover = 'lawsmodule[4].photo'
                    tempInfo = 'lawsmodule[4].info'
                }
                else if(i === 5){
                    temp = 'lawsmodule[5].collectionsNum'
                    tempCover = 'lawsmoule[5].photo'
                    tempInfo = 'lawsmodule[5].info'
                }
                else if(i === 6){
                    temp = 'lawsmodule[6].collectionsNum'
                    tempCover = 'lawsmodule[6].photo'
                    tempInfo = 'lawsmodule[6].info'
                }
                else if(i === 7){
                    temp = 'lawsmodule[7].collectionsNum'
                    tempCover = 'lawsmodule[7].photo'
                    tempInfo = 'lawsmodule[7].info'
                }
                
                that.setData({
                    [temp]: collectionsCount,
                    [tempCover]:tempCollections_cover,
                    [tempInfo]:tempCollections_info
                }) 
            }
            console.log("jie")
            // 进行排序
            for(let i = 0;i<3;i++){
                let tempSwap
                for(let j = i; j<4;j++){
                    if(that.data.lawsmodule[i].collectionsNum<that.data.lawsmodule[j].collectionsNum){
                        tempSwap = that.data.lawsmodule[i]
                        that.data.lawsmodule[i] = that.data.lawsmodule[j]
                        that.data.lawsmodule[j] = tempSwap
                    }
                }
            }
        })
    },
    // 加载农技教学数据
    updataFarmingRank:function(){
        let that = this
        let promise = new Promise((reslove,reject)=>{
            db.collection('farmingVideo').count().then(async res =>{
                let total = res.total;
                // 计算需分几次取
                const batchTimes = Math.ceil(total / 10)
                 // 清空临时数组
                that.setData({
                    temp_farmingList:[]
                })
                // 承载所有读操作的 promise 的数组
                for (let i = 0; i < batchTimes; i++) {
                    await db.collection('farmingVideo').skip(i * 10).limit(10).get().then(async res => {
                        let new_data = res.data
                        let old_data = that.data.temp_farmingList
                        that.setData({
                            temp_farmingList:old_data.concat(new_data)
                        })
                    })
                }
                reslove();
            })
        })
        promise.then(res=>{
            let temp_farmingList = that.data.temp_farmingList
            let farmingmodule = that.data.farmingmodule
            // 初始化数组长度(根据farmingmodule长度而定)
            let outArr=[]
            for(let i=0;i<3;i++){
                outArr.push([]);
            }
             // 将数据库的temp_farmingList数据进行分类存储
             for (let i = 0; i < temp_farmingList.length; i++) {
                let temp_flag = temp_farmingList[i].type
                for (let j = 0; j < 3; j++) {
                    if(temp_flag === farmingmodule[j].type){
                        outArr[j].push(temp_farmingList[i])
                        break
                    }   
                }
            }
            // 保存分完类的列表到lawsList
            that.setData({
                farmingList:outArr
            })

            // 计算总的播放量
            for(let i = 0; i<that.data.farmingList.length; i++){
                // 基础播放量
                let playCount = 0
                let tempPlay = 0
                let tempAvatarUrl
                let tempInfo
                for(let j = 0; j<that.data.farmingList[i].length; j++){
                    let single_playNum = that.data.farmingList[i][j].playNum
                    if(tempPlay <= single_playNum){
                        tempPlay = single_playNum
                        tempAvatarUrl = that.data.farmingList[i][j].avatarUrl
                        tempInfo = that.data.farmingList[i][j].info
                    }
                    console.log("加载前"+playCount)
                    playCount += single_playNum
                }
                console.log(playCount)
                let temp
                let tempCover
                let tempinfo
                if(i === 0){
                    temp = 'farmingmodule[0].playNum'
                    tempCover = 'farmingmodule[0].photo'
                    tempinfo = 'farmingmodule[0].info'
                }
                else if(i === 1){
                    temp = 'farmingmodule[1].playNum'
                    tempCover = 'farmingmodule[1].photo'
                    tempinfo = 'farmingmodule[1].info'
                }
                else if(i === 2){
                    temp = 'farmingmodule[2].playNum'
                    tempCover = 'farmingmodule[2].photo'
                    tempinfo = 'farmingmodule[2].info'
                }
                else if(i === 3){
                    temp = 'farmingmodule[3].playNum'
                    tempCover = 'farmingmodule[3].photo'
                    tempinfo = 'farmingmodule[3].info'
                }
                else if(i === 4){
                    temp = 'farmingmodule[4].playNum'
                    tempCover = 'farmingmodule[4].photo'
                    tempinfo = 'farmingmodule[4].info'
                }
                else if(i === 5){
                    temp = 'farmingmodule[5].playNum'
                    tempCover = 'farmingmoule[5].photo'
                    tempinfo = 'farmingmodule[5].info'
                }
                else if(i === 6){
                    temp = 'farmingmodule[6].playNum'
                    tempCover = 'farmingmodule[6].photo'
                    tempinfo = 'farmingmodule[6].info'
                }
                else if(i === 7){
                    temp = 'farmingmodule[7].playNum'
                    tempCover = 'farmingmodule[7].photo'
                    tempinfo = 'farmingmodule[7].info'
                }
                else if(i === 8){
                    temp = 'farmingmodule[8].playNum'
                    tempCover = 'farmingmodule[8].photo'
                    tempinfo = 'farmingmodule[8].info'
                }
                else if(i === 9){
                    temp = 'farmingmodule[9].playNum'
                    tempCover = 'farmingmodule[9].photo'
                    tempinfo = 'farmingmodule[9].info'
                }
                else if(i === 10){
                    temp = 'farmingmodule[10].playNum'
                    tempCover = 'farmingmodule[10].photo'
                    tempinfo = 'farmingmodule[10].info'
                }
                else if(i === 11){
                    temp = 'farmingmodule[11].playNum'
                    tempCover = 'farmingmodule[11].photo'
                    tempinfo = 'farmingmodule[11].info'
                }
                else if(i === 12){
                    temp = 'farmingmodule[12].playNum'
                    tempCover = 'farmingmodule[12].photo'
                    tempinfo = 'farmingmodule[12].info'
                }
                else if(i === 13){
                    temp = 'farmingmodule[13].playNum'
                    tempCover = 'farmingmodule[13].photo'
                    tempinfo = 'farmingmodule[13].info'
                }
                else if(i === 14){
                    temp = 'farmingmodule[14].playNum'
                    tempCover = 'farmingmodule[14].photo'
                    tempinfo = 'farmingmodule[14].info'
                }
                else if(i === 15){
                    temp = 'farmingmodule[15].playNum'
                    tempCover = 'farmingmodule[15].photo'
                    tempinfo = 'farmingmodule[15].info'
                }
                else if(i === 16){
                    temp = 'farmingmodule[16].playNum'
                    tempCover = 'farmingmodule[16].photo'
                    tempinfo = 'farmingmodule[16].info'
                }
                
                that.setData({
                    [temp]: playCount,
                    [tempCover]:tempAvatarUrl,
                    [tempinfo]:tempInfo
                }) 
            }
            console.log("jie")
            // 进行排序
            for(let i = 0;i<3;i++){
                let tempSwap
                for(let j = i; j<4;j++){
                    if(that.data.farmingmodule[i].playNum<that.data.farmingmodule[j].playNum){
                        tempSwap = that.data.farmingmodule[i]
                        that.data.farmingmodule[i] = that.data.farmingmodule[j]
                        that.data.farmingmodule[j] = tempSwap
                    }
                }
            }
        })
    },
    // 更新数据函数
    updataRank:function(){
        let that = this
        let promise = new Promise((reslove,reject)=>{
            that.updataCommunityRank()
            that.updataLawsRank()
            that.updataFarmingRank()
            setTimeout(() => {
                reslove()
            }, 7000);
        })
        promise.then(res=>{
            let arr1 = []
            for(let i = 0;i<3;i++){
                arr1[i] = that.data.hotmodule[i]
            }
            let arr2 = []
            for(let i = 0;i<3;i++){
                arr2[i] = that.data.lawsmodule[i]
            }
            let arr3 = []
            for(let i = 0;i<2;i++){
                arr3[i] = that.data.farmingmodule[i]
            }
            
            // 调用formatTime函数，传入new Date()参数，返回当前日期和时间
            let date = util.formatTime(new Date());
            db.collection('hot_data').add({
                data:{
                    community_rank:arr1,
                    laws_rank:arr2,
                    farming_rank:arr3,
                    createTime:date
                }
                    
            }).then(res=>{
                wx.showToast({
                    title: '更新成功',
                    icon: 'true',
                    duration: 2000,
                })
            }).catch(err=>{
                console.log("添加失败")
                wx.showToast({
                    title: '更新失败',
                    icon: 'true',
                    duration: 2000,
                })
            })
        })
        
    },
    jumpgoback:function(){
        wx.navigateBack({ 
            changed: true
         })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        hot_data
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})